package com.dbg.utils;

import com.dbg.entity.Language;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;

public class JSONUtils {

    private static final Gson gson = new Gson();
    private static final Type languageListType = new TypeToken<List<Language>>() {}.getType();
    private static final Type patternsListType = new TypeToken<List<String>>() {}.getType();
    private static final Type multiPatternsListType = new TypeToken<List<Integer>>() {}.getType();

    public static final List<String> patternsList = JSONUtils.readPatterns();
    public static final List<Integer> multiPatternsList = JSONUtils.readMultiPattern();
    public static final List<Language> languagesList = JSONUtils.readLanguages();

    // 获取语言json对象数组
    private static List<Language> readLanguages() {

        InputStream resourceAsStream = JSONUtils.class.getClassLoader().getResourceAsStream("json/language.json");
        if (resourceAsStream != null) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
            return gson.fromJson(bufferedReader, languageListType);
        }
        return null;
    }

    // 获取规则数组
    private static List<String> readPatterns() {

        InputStream resourceAsStream = JSONUtils.class.getClassLoader().getResourceAsStream("json/pattern.json");
        if (resourceAsStream != null) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
            return gson.fromJson(bufferedReader, patternsListType);
        }
        return null;
    }

    // 获取多行数组下标
    private static List<Integer> readMultiPattern() {

        InputStream resourceAsStream = JSONUtils.class.getClassLoader().getResourceAsStream("json/multipattern.json");
        if (resourceAsStream != null) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
            return gson.fromJson(bufferedReader, multiPatternsListType);
        }
        return null;

    }

    // 测试方法
    public static void main(String[] args) {
        List<Language> languages = JSONUtils.readLanguages();
        if (languages != null) {
            for (Language language : languages) {
                System.out.println("ID: " + language.getId());
                System.out.println("Name: " + language.getName());
                System.out.println("File Name: " + language.getFileName());
                System.out.println("Patterns ID: " + language.getPatternsId());
                System.out.println("----------------------------");
            }
        } else {
            System.out.println("Failed to read languages.");
        }
        List<String> patterns = JSONUtils.readPatterns();
        if (patterns != null) {
            for (String pattern : patterns) {
                System.out.println(pattern);
            }
        }
        List<Integer> integers = JSONUtils.readMultiPattern();
        if (integers != null) {
            for (Integer integer : integers) {
                System.out.println(integer);
            }
        }
    }

}
